package com.heima.article.listener;

import com.heima.article.service.ApArticleService;
import com.heima.common.constants.message.WmNewsUpDownConstants;
import com.heima.common.util.JsonUtils;
import com.heima.model.media.dtos.WmNewsDownOrUpDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Optional;

/**
 * 监听WmNews上下架状态的kafka消息监听器
 *
 * @author Fuqiang
 * @since :2023-03-29
 */
@Slf4j
@Component
public class WmNewsUpOrDownListener {

    @Autowired
    private ApArticleService articleService;

    @KafkaListener(topics = WmNewsUpDownConstants.WM_NEWS_UP_OR_DOWN_TOPIC)
    public void receiveMsg(ConsumerRecord<String, String> record) {

        Optional<ConsumerRecord<String, String>> optional = Optional.ofNullable(record);
        if (optional.isPresent()) {
            String json = record.value();
            log.info("article服务接收到kafka消息:自媒体文章上下架,value:{}", json);
            WmNewsDownOrUpDTO newsDownOrUpDTO = JsonUtils.toBean(json, WmNewsDownOrUpDTO.class);
            articleService.updateIsDownById(newsDownOrUpDTO);
        }
    }

}
